﻿using GuanQL.Core;
using GuanQL.Core.Domain.Accounts;
using GuanQL.Core.Domain.Medias;
using GuanQL.Core.Stores;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Claims;
using System.Threading.Tasks;
using ZhongJia.Core.Utility;

namespace GuanQL.DataMigration.Seeds
{
    public class UserSeed : BaseSeed
    {
        private readonly UserManager<User> _userManager;

        public UserSeed(
            UserManager<User> userManager,
            GQLDbContext dbContext) : base(dbContext)
        {
            _userManager = userManager;
        }

        public override async Task Seed()
        {
            foreach (var member in typeof(GlobleConsts.Role).GetTypeInfo().GetMembers().Where(m => m.MemberType == MemberTypes.Field))
            {
                _dbContext.Set<Role>().Add(new Role
                {
                    TenantId = TenantId,
                    Name = member.Name,
                    NormalizedName = member.Name,
                    ConcurrencyStamp = Guid.NewGuid().ToString(),
                });
            }

            var avatars = GetRootPicture(Path.Combine(Directory.GetCurrentDirectory(), "Pictures/Avatars"), TenantId, true).ToList();
            _dbContext.Set<Picture>().AddRange(avatars);
            Consts.Avatars = avatars;
            await CreateUser(avatars, "admin", "admin", GlobleConsts.Role.Admin);
            for (int i = 0; i < 10; i++)
            {
                var user = await CreateUser(avatars, "1380013800" + i, "1380013800" + i, GlobleConsts.Role.Register, name: $"业主{i}", phoneNumberConfirmed: true);
                Consts.RegisterUsers.Add(user);
            }
            await CreateUser(avatars, "agent", "agent", GlobleConsts.Role.Agents);
        }

        private async Task<User> CreateUser(
            IList<Picture> avatars, string userName, string phoneNumber, string role, string name = null, bool phoneNumberConfirmed = false)
        {
            var user = new User
            {
                AvatarId = RandomHelper.GetRandomOf(avatars.ToArray()).Id,
                UserName = userName,
                TenantId = TenantId,
                PhoneNumber = phoneNumber,
                PhoneNumberConfirmed = phoneNumberConfirmed,
                Name = name
            };
            //if (role == GlobleConsts.Role.Agents)
            //{
            //    user.Agent = new Agent
            //    {
            //        DistrictId = Consts.DistrictId
            //    };
            //}
            user.Claims.Add(new IdentityUserClaim<Guid>
            {
                ClaimType = ClaimTypes.UserData,
                ClaimValue = TenantId.ToString()
            });
            user.Claims.Add(new IdentityUserClaim<Guid>
            {
                ClaimType = ClaimTypes.Role,
                ClaimValue = role
            });
            var result = await _userManager.CreateAsync(user, "1234abcd");
            if (result.Succeeded)
            {
                await _userManager.AddToRoleAsync(user, role);
            }
            return user;
        }
    }
}
